
#include "bsp_usart.h"
#include "bsp_key.h"
#include <math.h>

/* Encoder.c */
//extern double abs(double __x); 

u16	Parameter = 1000;																

struct FlashData FlashData_Copy[20] ; 

//初始化编码开关参数
void encoder_count_init(void)
{
	uint8_t i = 0;
	for(i=0;i<20;i++)
	{	
			FlashData_Copy[i].Phase_A = 0;				
			FlashData_Copy[i].Phase_B = 0;				
			FlashData_Copy[i].encoder_count = 0;			
			FlashData_Copy[i].Old_encoder_count = 0;	
	}
}
void EncoderA(uint8_t encoder_A ,uint8_t mun )
{
	if(encoder_A)
	{
		FlashData_Copy[mun].Phase_B |= 0x80; //Phase_B = 1xxx xxxx
	}
	else
	{
		FlashData_Copy[mun].Phase_B  &= 0x7F; //Phase_B = 0xxx xxxx
	}
	if(((FlashData_Copy[mun].Phase_B  << 1)^FlashData_Copy[mun].Phase_B ) & 0x80) //判断缓存中的bit7位和bit6位是否相等，若不等，更新缓存
	{
		FlashData_Copy[mun].Phase_A  >>= 1;
		FlashData_Copy[mun].Phase_B  >>= 1;
		if( ((FlashData_Copy[mun].Phase_A & 0x70) == 0x30) && ((FlashData_Copy[mun].Phase_B & 0x70) == 0x60) ) //A:0 1 1, B:1 1 0 序列
		{
			if(FlashData_Copy[mun].encoder_count < Parameter)
				FlashData_Copy[mun].encoder_count += 1;
		}
		if( ((FlashData_Copy[mun].Phase_A & 0x70) == 0x60) && ((FlashData_Copy[mun].Phase_B & 0x70) == 0x30) ) //A:1 1 0, B:0 1 1 序列
		{
			if(FlashData_Copy[mun].encoder_count > 0)
				FlashData_Copy[mun].encoder_count -= 1;
		}
	}
}
void EncoderB(uint8_t encoder_B,uint8_t mun)
{
	if(encoder_B)
	{
		FlashData_Copy[mun].Phase_A |= 0x80; //Phase_A = 1xxx xxxx
	}
	else
	{
		FlashData_Copy[mun].Phase_A &= 0x7F; //Phase_A = 0xxx xxxx
	}
	if(((FlashData_Copy[mun].Phase_A << 1)^FlashData_Copy[mun].Phase_A ) & 0x80)
	{
		FlashData_Copy[mun].Phase_A >>= 1;
		FlashData_Copy[mun].Phase_B >>= 1;
		if( ((FlashData_Copy[mun].Phase_A & 0x70) == 0x30) && ((FlashData_Copy[mun].Phase_B & 0x70) == 0x60) )
		{
			if(FlashData_Copy[mun].encoder_count < Parameter)
				FlashData_Copy[mun].encoder_count += 1;
		}
		if( ((FlashData_Copy[mun].Phase_A & 0x70) == 0x60) && ((FlashData_Copy[mun].Phase_B & 0x70) == 0x30) )
		{
			if(FlashData_Copy[mun].encoder_count > 0)
				FlashData_Copy[mun].encoder_count -= 1;
		}
	}
}
//加入去抖算法
void Encoder_AB(uint8_t encoder_A ,uint8_t encoder_B , uint8_t mun )
{
	EncoderA(encoder_A,mun);
	EncoderB(encoder_B,mun);
}
	
void Encoder_1(uint8_t encoder_A ,uint8_t encoder_B , uint8_t mun )
{
	if(!encoder_A)
	{
			if(encoder_B){
				if(FlashData_Copy[mun].encoder_count < Parameter)
					FlashData_Copy[mun].encoder_count++;
			}else{
				if(FlashData_Copy[mun].encoder_count > 0)
					FlashData_Copy[mun].encoder_count--;
			}
	}
}
#if 		HUODAO_OPTION
// 编码开关没有加入去抖算法
void Encoder_Scan_1(void)
{
	Encoder_1(READ_KEY7,READ_KEY8,0);
	Encoder_1(READ_KEY5,READ_KEY6,1);
	Encoder_1(READ_KEY3,READ_KEY4,2);
}
u16 count,Old_Temp,Temp;

//编码开关加入去抖算法
void Encoder_Scan(void)
{
	Encoder_AB(READ_KEY5,READ_KEY6,0);
	Encoder_AB(READ_KEY3,READ_KEY4,1);
	Encoder_AB(READ_KEY7,READ_KEY8,2);
	
	Old_Temp = Temp;		
	Temp = FlashData_Copy[0].encoder_count;	
	if(Old_Temp != Temp)										
	{
			count = Temp;
			if(count>7&&count<15){
					goods_state[0].expenditure = 1; 
			}else if(count>15&&count<23){
					goods_state[0].expenditure = 2; 
			}else if(count>23&&count<31){
					goods_state[0].expenditure = 4; 
			}else if(count>31&&count<39){
					goods_state[0].expenditure = 5; 
			}
	}			
}
//保存当前计数值
void Encoder_count_old(void)
{
	uint8_t mun = 0;
	for(mun=0;mun<20;mun++)
	{
		FlashData_Copy[mun].Old_encoder_count = FlashData_Copy[mun].encoder_count;//保存当前计数值
	}
}
//编码计数结算商品数量
void Encoder_count_balance(void)
{
		uint8_t mun = 0;
		uint16_t count = 0;
		Old_Temp = Temp;		
	
			Temp = FlashData_Copy[0].encoder_count;	
		if(Old_Temp != Temp)	{
			count = FlashData_Copy[mun].encoder_count;
			if(count>7&&count<15){
					goods_state[mun].expenditure = 1; 
			}else if(count>15&&count<23){
					goods_state[mun].expenditure = 2; 
			}else if(count>23&&count<31){
					goods_state[mun].expenditure = 4; 
			}else if(count>31&&count<39){
					goods_state[mun].expenditure = 5; 
			}
			
#if DEBUG_LOG
			printf("%d encoder = %d\n",mun,goods_state[mun].expenditure);		 //打印消费数量
#endif //DEBUG_LOG
		}
	}
}

#endif

